package com.kugoweb.shthon2011s.tlwatch.commons;

import java.util.HashMap;
import java.util.Map;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SettingsData extends SQLiteOpenHelper {

	private static final String DB_NAME = "settings_db";
	private static final int DB_VERSION = 1;

	private static final String TBL_NAME = "settings";
	private static final String ITEM_NAME = "item_name";
	private static final String ITEM_VALUE = "item_value";
	private static final String CREATE_TBL = "create table if not exists "
			+ TBL_NAME + " (" // 
			+ ITEM_NAME + " text primary key," //
			+ ITEM_VALUE + " text not null" //
			+ ")";
	private static final String[] COLUMNS = { ITEM_NAME, ITEM_VALUE };
	private static final String CONDITION_NAME = ITEM_NAME + " = ?";

	private static final String NICK_NAME = "nick_name";
	private static final String USER_ID = "user_id";
	private static final String SENTENCE_VERSION = "sentence_version";
	private static final String TWITTER_TOKEN = "twitter_token";
	private static final String TWITTER_SECRET = "twitter_secret";
	private static final String TWITTER_NAME = "twitter_name";
	private static final String TWITTER_USE_AUTO_RANKING = "twitter_use_auto_ranking";
	private static final String TWITTER_USE_AUTO_HIGH_SCORE = "twitter_use_auto_high_score";
	private static final String LAST_VERSION = "last_version";

	private final Map<String, String> mItemMap;

	/**
	 * 最後に起動したときのバージョンを返す
	 * 
	 * @return
	 */
	public int getLastVersion() {
		final String value = mItemMap.get(LAST_VERSION);
		if (value == null) {
			return 0;
		} else {
			return Integer.parseInt(value);
		}
	}

	/**
	 * 最後に起動したときのバージョンを保存する
	 * 
	 * @param lastVersion
	 */
	public void setLastVersion(final int lastVersion) {
		saveItem(LAST_VERSION, lastVersion + "");
	}

	/**
	 * @return
	 */
	public boolean isTwitterUseAutoHighScore() {
		final String value = mItemMap.get(TWITTER_USE_AUTO_HIGH_SCORE);
		if (value == null) {
			return true;
		} else {
			return Boolean.parseBoolean(value);
		}
	}

	/**
	 * @param useAutoHighScore
	 */
	public void setTwitterUseAutoHighScore(final boolean useAutoHighScore) {
		saveItem(TWITTER_USE_AUTO_HIGH_SCORE, useAutoHighScore + "");
	}

	/**
	 * @return
	 */
	public boolean isTwitterUseAutoRanking() {
		final String value = mItemMap.get(TWITTER_USE_AUTO_RANKING);
		if (value == null) {
			return true;
		} else {
			return Boolean.parseBoolean(value);
		}
	}

	/**
	 * @param useAutoRanking
	 */
	public void setTwitterUseAutoRanking(final boolean useAutoRanking) {
		saveItem(TWITTER_USE_AUTO_RANKING, useAutoRanking + "");
	}

	/**
	 * Twitter Screen Nameを返す
	 * 
	 * @return
	 */
	public String getTwitterScreenName() {
		return mItemMap.get(TWITTER_NAME);
	}

	/**
	 * Twitter Screen Nameを保存する
	 * 
	 * @param screenName
	 */
	public void setTwitterScreenName(final String screenName) {
		saveItem(TWITTER_NAME, screenName);
	}

	/**
	 * Twitter Tokenを返す
	 * 
	 * @return
	 */
	public String getTwitterSecret() {
		return mItemMap.get(TWITTER_SECRET);
	}

	/**
	 * Twitter Secretを保存する
	 * 
	 * @param twitterSecret
	 */
	public void setTwitterSecret(final String twitterSecret) {
		saveItem(TWITTER_SECRET, twitterSecret);
	}

	/**
	 * Twitter Tokenを返す
	 * 
	 * @return
	 */
	public String getTwitterToken() {
		return mItemMap.get(TWITTER_TOKEN);
	}

	/**
	 * Twitter Tokenを保存する
	 * 
	 * @param twitterToken
	 */
	public void setTwitterToken(final String twitterToken) {
		saveItem(TWITTER_TOKEN, twitterToken);
	}

	/**
	 * 文章問題のバージョンを返す
	 * 
	 * @return
	 */
	public int getSentenceVersion() {
		final String sVersion = mItemMap.get(SENTENCE_VERSION);
		if (Utils.isEmpty(sVersion)) {
			return 0;
		} else {
			try {
				return Integer.parseInt(sVersion);
			} catch (final NumberFormatException e) {
				return 0;
			}
		}
	}

	/**
	 * 文章問題のバージョンをセットする
	 * 
	 * @param sentenceVersion
	 */
	public void setSentenceVersion(final int sentenceVersion) {
		saveItem(SENTENCE_VERSION, sentenceVersion + "");
	}

	/**
	 * User IDを返す
	 * 
	 * @return
	 */
	public String getUserId() {
		return mItemMap.get(USER_ID);
	}

	/**
	 * User IDを保存する
	 * 
	 * @param userId
	 */
	public void setUserId(final String userId) {
		saveItem(USER_ID, userId);
	}

	/**
	 * NickNameを返す
	 * 
	 * @return
	 */
	public String getNickName() {
		return mItemMap.get(NICK_NAME);
	}

	/**
	 * NickNameを保存する
	 * 
	 * @param nickName
	 */
	public void setNickName(final String nickName) {
		saveItem(NICK_NAME, nickName);
	}

	/**
	 * コンストラクタ
	 * 
	 * @param context
	 */
	public SettingsData(final Context context) {
		super(context, DB_NAME, null, DB_VERSION);
		mItemMap = new HashMap<String, String>();
		final SQLiteDatabase db = this.getWritableDatabase();
		final Cursor cursor = db.query(TBL_NAME, COLUMNS, null, null, null,
				null, null);
		while (cursor.moveToNext()) {
			mItemMap.put(cursor.getString(0), cursor.getString(1));
		}
		cursor.close();
		db.close();
	}

	/**
	 * Itemを保存する
	 * 
	 * @param name
	 * @param value
	 */
	private void saveItem(final String name, final String value) {
		final SQLiteDatabase db = this.getWritableDatabase();
		final ContentValues values = new ContentValues(2);
		values.put(ITEM_NAME, name);
		values.put(ITEM_VALUE, value);
		final int row = db.update(TBL_NAME, values, CONDITION_NAME,
				new String[] { name });
		if (row < 1) {
			db.insert(TBL_NAME, null, values);
		}
		db.close();
		mItemMap.put(name, value);
	}

	@Override
	public void onCreate(final SQLiteDatabase db) {
		db.execSQL(CREATE_TBL);
	}

	@Override
	public void onUpgrade(final SQLiteDatabase db, final int oldVersion,
			final int newVersion) {
		// do nothing
	}

}
